from tools import read_jscode
import requests
import time
from urllib.parse import urlencode
from beeize.scraper import Scraper
import os

# os.environ["websession".upper()]="040069b3a84c9f2f0edcf4b2be344bf494a1f1"
# os.environ["note_id".upper()]="668d2fea00000000250164b0"

scraper = Scraper()
_input = scraper.input

websession = _input.get_string('websession')  # 获取 websession 输入
note_id = _input.get_string('note_id')  # 获取 note_id 输入

ids = {}

cookies = {
    'a1': '1904a59f643ijsev1768u64b2rf4h8yb84pzqvv5l50000405869',
    'web_session': websession,

}
headers = {
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh-CN,zh;q=0.9',
    'cache-control': 'no-cache',
    # 'cookie': 'abRequestId=aecbdc8d-eb08-5d08-bb2e-bde25338b8e3; a1=1904a59f643ijsev1768u64b2rf4h8yb84pzqvv5l50000405869; webId=e9635e5801749021f7f2b76fb462c158; gid=yj82jjddWYYqyj8402jiK4hh4q3VMdhyWKY7U21K4DJFiu284xYvDA888482YKj8WYDfid4q; xsecappid=xhs-pc-web; webBuild=4.24.2; acw_tc=6e1a63120046d60e39b5095d0eb384bf36282aaa433fa0ceac1955b59ad9f056; web_session=040069b3a84c9f2f0edcf4b2be344bf494a1f1; unread={%22ub%22:%2266866aa4000000001f007c16%22%2C%22ue%22:%226688bc5b000000001e010158%22%2C%22uc%22:29}; websectiga=f3d8eaee8a8c63016320d94a1bd00562d516a5417bc43a032a80cbf70f07d5c0; sec_poison_id=01fc6da3-efdf-4939-be34-47479d869d97',
    'origin': 'https://www.xiaohongshu.com',
    'pragma': 'no-cache',
    'priority': 'u=1, i',
    'referer': 'https://www.xiaohongshu.com/',
    'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
    'x-b3-traceid': '4f1c864e987a7cdd',
    'x-t': str(int(time.time()*1000)),
}
cursor=''
for i in range(0,10):
    params={
    'note_id': note_id,
    'cursor': '',
    'top_comment_id': cursor,
    'image_formats': 'jpg,webp,avif',
}
    newparams=urlencode(params)
    xs=read_jscode('main.js','hoot_fn','/api/sns/web/v2/comment/page?'+newparams)
    headers['x-s']=xs
    response = requests.get(
        'https://edith.xiaohongshu.com/api/sns/web/v2/comment/page?',
        cookies=cookies,
        headers=headers,
        params=params
    )
    try:
        alldata = response.json()['data']['comments']
        for i in alldata:
            if i['id'] not in ids:
                print(i)
                scraper.push_data(i)
            ids[i['id']] = None
        cursor = response.json()['data']['cursor']
    except:
        pass

